package managers
{
	import com.demonsters.debugger.MonsterDebugger;
	
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	import org.osmf.net.StreamingURLResource;
	
	import utils.LoaderResource;

	public class SoundManager
	{
		public static const BUTTON_CLICK:String = "btnClick";
		public static const FON1_SOUND:String 	= "Fon1";
		public static const FON2_SOUND:String 	= "Fon2";
		private static var _instance:SoundManager;
		public function SoundManager()
		{
			
		}
		public static function getInstance():SoundManager{
			if(!_instance){
				_instance = new SoundManager();
			}
			return _instance;
		}
		
		private var _idSound:String = "";
		private var _repeat:int=0;
		private var sounds:Array= new Array();
		
		public function playSound(idSound:String,repeat:int=0):void
		{
			_idSound = idSound;
			_repeat = repeat;
			if(LoaderResource.instance.assetManager.getSound(idSound)){
				if(repeat>0){
					sounds[_idSound] = LoaderResource.instance.assetManager.playSound(_idSound,0,_repeat+999);
				}
				else{
					sounds[_idSound] = LoaderResource.instance.assetManager.playSound(_idSound,0,0);
				}
			}
			else{
				LoaderResource.instance.assetManager.enqueue(getPathSound(_idSound));
				LoaderResource.instance.assetManager.loadQueue(onCompleteLoad);
			}
		}
		
		public function stopSound(idSound:String):void{
			MonsterDebugger.trace("stopSound",sounds);
			if(sounds[idSound]){
				sounds[idSound].stop();
				delete sounds[idSound];
			}
		}
		
		private function onCompleteLoad(ratio:Number):void{
			if(ratio == 1){
				if(_repeat>0){
					sounds[_idSound] = LoaderResource.instance.assetManager.playSound(_idSound,0,999);
				}
				else{
					sounds[_idSound] = LoaderResource.instance.assetManager.playSound(_idSound,0,0);
				}
			}
			
		}
		
		public function getPathSound(idSound:String):File{
			var rez:File = new File();
			switch(idSound)
			{
				case BUTTON_CLICK:
				{
					rez = File.applicationDirectory.resolvePath("../bin-debug/sounds/btnClick.mp3");
					break;
				}
				case FON1_SOUND:
				{
					rez = File.applicationDirectory.resolvePath("../bin-debug/sounds/Fon1.mp3");
					break;
				}
				case FON2_SOUND:
				{
					rez = File.applicationDirectory.resolvePath("../bin-debug/sounds/Fon2.mp3");
					break;
				}
			}
			return rez;
		}
	}
}